<?php
namespace Dyadmin\Controller;
use Think\Controller;
class InvitationCodeController extends CommomController{
    public function index()
    {
        $this->check('12_0');
        $title="邀请码管理";
        $key       =   I('key');
        $add_se_time= I('add_se_time');
        $deal_se_time= I('deal_se_time');
        $prefix=I('prefix');
        $status= I('status');
        $is_end= I('is_end');

        if ($add_se_time != "") {
            $map['add_se_time']=$add_se_time;
            $times = explode("~", $add_se_time);
            $map['add_time']  = array('between',array(strtotime($times[0]),strtotime($times[1])));
        }
        if ($deal_se_time != "") {
            $map['deal_se_time']=$deal_se_time;
            $times = explode("~", $deal_se_time);
            $map['deal_time']  = array('between',array(strtotime($times[0]),strtotime($times[1])));
        }
        if ($prefix != "") {
            $map['prefix']  = strtoupper($prefix);
        }
        if ($status!=="") {
            $map['status']  =$status;
        }
        if ( $is_end !=="") {
            if($is_end=="0"){
                $map['end_time']  = array('egt',time());
                $map['is_end']  =0;
            }
            if($is_end=="1"){
                $map['end_time']  = array('lt',time());
                $map['is_end']  =1;
            }

        }
        if(!empty($key)){
            $map['code|login_name']    =   array('like', '%'.(string)$key.'%');
            $map['key'] =$key;
        }

        $count = M('invitation_code')->where($map)->count();
        unset($map['code|login_name']);
        $page = new \Think\Page ($count, 15, $map);
        if (!empty(I("clear_set"))) {
            $page->firstRow = 0;
            $page->listRows = 15;
        }
        //显示分页
        $show = $page->show();
        $data = M('invitation_code')
            ->where($map)->order('id asc')
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();
        $this->assign('page', $show);

        $this->assign('map', $map);
        $this->assign('data', $data);
        $this->assign('title', $title);
        $this->display();
    }

    public function add()
    {
        $this->check('12_1');
        $title="添加邀请码";
        if(IS_POST) {
            $login_name = I("login_name");
            $code_num = I("code_num");
            $prefix = I("prefix");
            $code_len=I("code_len");
            $end_time = I("end_time");
            $res=D("InvitationCode")->addcode($code_num,$prefix,$end_time,$login_name,$code_len);
            if($res['status']==1){
                $this->success('添加成功！',U('InvitationCode/index'));
            }else{
                $this->error($res['error']);
            }
        }
        $this->assign("title",$title);
        $this->display();
    }

    /**
     * 删除邀请码
     * @author zgc
     */
    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'delcode':
                $map['status']=0;
                $this->delete('InvitationCode', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }
}